package com.sat.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sat.goods.mapper.SpecMapper;
import com.sat.goods.model.Spec;
import com.sat.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements SpecService {

    @Autowired
    private SpecMapper specMapper;

    @Override
    public List<Spec> findList(Spec spec) {
        QueryWrapper<Spec> queryWrapper = getAlbumQueryWrapper(spec);
        return specMapper.selectList(queryWrapper);
    }

    @Override
    public Page findPage(Integer page, Integer size) {
        Page<Spec> specPage = new Page<>(page, size);
        return specMapper.selectPage(specPage,null);
    }

    @Override
    public Page findPage(Integer page, Integer size, Spec spec) {
        Page<Spec> specPage = new Page<>(page, size);
        QueryWrapper<Spec> specQueryWrapper = getAlbumQueryWrapper(spec);
        return specMapper.selectPage(specPage,specQueryWrapper);
    }

    private QueryWrapper<Spec> getAlbumQueryWrapper(Spec spec) {
        QueryWrapper<Spec> queryWrapper = new QueryWrapper<>();
        if (spec != null) {
            if (spec.getName() != null && !"".equals(spec.getName())) {
                queryWrapper.like("name", spec.getName());
            }
        }
        return queryWrapper;
    }
}
